import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//查询
public class Demo3 {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test_rjw?CharacterEncoding&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("200483");

        Connection connection=dataSource.getConnection();

        String sql = "select name from student where id = ?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setInt(1,1);

        // 注意这里返回的是结果集
        ResultSet resultSet = statement.executeQuery();

        // 通过next方法就可以获取到临时表中的每一行数据，如果获取到最后一行之后，再执行next返回false，循环结束
        while(resultSet.next()){
//            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(name);
        }

        resultSet.close();
        statement.close();
        connection.close();

    }
}
